# -*- coding: utf-8 -*-
import traceback, os, datetime
import cherrypy
import urllib
import HTMLParser
import cStringIO as StringIO
from urlparse import urlparse
from urlparse import urljoin
from Cheetah.Template import Template
from readability.readability import Document
from lxml import etree
from termcolor import cprint

class Browser():
    @cherrypy.expose
    def index(self):
        t = Template(file='browser_template.html', searchList=[{'lurl': '', 'title': '', 'content': ''}])
        return str(t)

    @cherrypy.expose
    def doProcess(self, text=None):
        try:
            if text==None or text=='': cherrypy.HTTPRedirect('/')
            tmp = text.split()
            print tmp
            data = []; title = ''; content = ''; tmp_url = ''
            for lurl in tmp:
                try:
                    if lurl!=None and not lurl.startswith('http'): 
                        lurl = '{0}{1}'.format('http://', lurl)
                    tmp_url  += lurl + ' '
                    print 'URL: ', lurl
                    html    = urllib.urlopen(lurl).read()
                    content = Document(html).summary()
                    #print 'HTML: ', content
                    title   = Document(html).short_title()
                    content = self.doProcessImage(content, lurl)
                    print '##################################################'
                    #print 'Content', content
                    data.append({'lurl': lurl, 'title': title, 'content': content})
                except:
                    traceback.print_exc()
                    data.append({'lurl': lurl, 'title': title, 'content': content})
                    continue
            t = Template(file='browser_template.html', searchList=[{'lurl': tmp_url, 'data': data}])
            return str(t)
        except:
            traceback.print_exc()
            raise cherrypy.HTTPRedirect("/")

    @cherrypy.expose
    def doProcessImage(self, html=None, link=None):
        try:
            parser      = etree.XMLParser(encoding='utf-8')
            html        = html.replace('<body/>', '')
            if type(html).__name__ == 'unicode': html = html.encode('utf-8')
            tree        = etree.parse(StringIO.StringIO(html), parser)
            html        = unicode(html, 'utf-8')
            hparser     = HTMLParser.HTMLParser()
            html        = hparser.unescape(html)
            imageNode   = tree.xpath('.//img')
            for img in imageNode:
                href = img.get('src')
                if not href.startswith('http'):
                    cprint(href, 'green')
                    href_fix = urljoin(link, href)
                    if type(href_fix).__name__=='unicode': href_fix = href_fix.encode('utf-8')
                    href_fix = unicode(href_fix, 'utf-8')
                    cprint(href_fix, 'yellow')
                    html     = html.replace(href, href_fix)
            return html
        except:
            traceback.print_exc()
            return html

def startProgram():
    currentDir = os.path.dirname(os.path.abspath(__file__))
    cherrypy.config.update({'server.socket_port':8888, 
        'server.socket_host':'0.0.0.0', 
        'engine.autoreload.on': True, 
        'tools.sessions.on': True,
        'tools.encode.encoding': 'utf-8',
        'server.environment': 'development'})
    conf = {
                '/': {
                    'tools.staticdir.on': True,
                    'tools.staticdir.dir': currentDir,
                },
           }

    cherrypy.quickstart(Browser(), '/', config=conf)        

if __name__=='__main__':
    startProgram()
